import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';

/// 通用列表对话框
///
/// @author 周卓
///
class KqListDialog<T> extends AlertDialog {
  /// 标题，非必传
  final String? customTitle;

  /// 数据源
  final List<T> items;

  /// 转换 T -> String 转换
  final String Function(int index, T item)? nameConvert;

  /// 自定义Item，特殊情况使用这个
  final Widget Function(int index, T item)? customItemBuilder;

  /// 回调
  final Function(int index, T item)? callback;

  const KqListDialog(
      {Key? key,
      required this.items,
      this.customTitle,
      this.nameConvert,
      this.customItemBuilder,
      this.callback})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    // 小诸葛规范，最多显示6个，多余滚动
    var maxHeight = 6 * 88.r;

    // 标题

    Widget? titleWidget;
    if (customTitle != null) {
      titleWidget = Container(
        height: 88.r,
        padding: EdgeInsets.symmetric(horizontal: 20.r),
        child: Row(
          children: [
            Expanded(
              child: Text(
                customTitle!,
                maxLines: 1,
                textAlign: TextAlign.center,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 28.sp,
                  color: KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .titleColor ??
                      KqThemeColors.textBlack85,
                ),
              ),
            )
          ],
        ),
      );
    }

    /// item
    for (int i = 0; i < items.length; i++) {
      var item = items[i];
      String? title;
      if (customItemBuilder == null) {
        if (nameConvert != null) {
          title = nameConvert!(i, item);
        } else {
          if (item is String) {
            title = item;
          } else if (item is Map) {
            title = item['title'];
          } else {
            title = '';
          }
        }
      }
      children.add(KqInkWell(
        onTap: () => {
          if (callback != null) {callback!(i, item)}
        },
        child: customItemBuilder != null
            ? customItemBuilder!(i, item)
            : Container(
                height: 88.r,
                alignment: Alignment.center,
                width: double.infinity,
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 20.r),
                child: Text(
                  title ?? '',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      fontSize: 24.sp,
                      color: KqThemeManager.instance
                              .getConfig()
                              .dialogConfig
                              .itemTextColor ??
                          KqThemeColors.text26),
                ),
              ),
      ));
      if (i < (items.length - 1)) {
        children.add(KqDivider(
          color: Colors.black.withOpacity(0.1),
        ));
      }
    }

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    return UnconstrainedBox(
        child: SizedBox(
            width: 880.r,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4.r))),
                color:
                    KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
                        KqThemeColors.bgDialog,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(4.r)),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (titleWidget != null) titleWidget,
                      if (titleWidget != null)
                        KqDivider(
                          color: Colors.black.withOpacity(0.1),
                        ),
                      LimitedBox(
                        maxHeight: maxHeight,
                        child: SingleChildScrollView(
                          child: dialogChild,
                        ),
                      ),
                    ],
                  ),
                ))));
  }
}
